package com.zhenyi.tuangou.index;

import com.jfinal.kit.Kv;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Record;
import com.jfinal.plugin.activerecord.SqlPara;
import com.zhenyi.common.model.TgUser;
import com.zhenyi.tuangou.core.WXPayUtils;

import cn.hutool.core.lang.Singleton;

public class OrderSingleton extends TGController {
	private boolean isOn = false;

	public static OrderSingleton getintsance() {
		return Singleton.get(OrderSingleton.class);
	}

	public void turnOn() {
		isOn = true;

	}

	public void turnOff() {
		isOn = false;

	}

	public boolean isOn() {
		return isOn;
	}

	/**
	 * 单独购买，用户自行发起退款。
	 * @param productId
	 * @param orderId
	 */
	public void toRefund(Long productId, Long orderId) {
		TgUser user = getUser();
		Kv cond = Kv.by("userId", user.getId());
		cond.set("productId", productId);
		cond.set("orderId", orderId);
		SqlPara sql = Db.getSqlPara("findOrderWithToRefund", cond);
		Record record = Db.findFirst(sql);
		if (record == null) {
			renderError(500);
			return;
		}
		WXPayUtils wxPayUtils = new WXPayUtils();
		String outTradeNo = record.getStr("outTradeNo");
		int total = record.getInt("totalFee");
		wxPayUtils.refund(outTradeNo, total);
	}

}
